package com.zds.support.api


import android.text.TextUtils
import androidx.lifecycle.LifecycleOwner
import com.zds.support.api.req.*
import com.zds.support.api.resp.*
import com.zds.support.api.retrofit.bindToHttpRequestListener
import com.zds.support.api.retrofit.exception.ApiException
import com.zds.support.api.retrofit.function.ApiResponseFunction
import com.zds.support.api.retrofit.function.ExceptionEngine
import com.zds.support.api.retrofit.interceptor.ApiInterceptor
import com.zds.support.api.retrofit.security.SSL
import com.zds.support.base.SupportBaseApplication
import com.zds.support.constant.UrlConstatns
import com.zds.support.manager.CacheManager
import com.zds.support.manager.UserDataManager
import com.zds.support.util.AppUtils
import com.zds.support.util.FileUtils
import io.reactivex.Observable

import io.reactivex.disposables.Disposable
import io.reactivex.plugins.RxJavaPlugins
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File

import java.net.Proxy
import java.util.concurrent.TimeUnit
import kotlin.collections.HashMap

class ApiManager {
    private val retrofit: Retrofit
    private val apiService: ApiService
    private val okHttpClient: OkHttpClient
    private val loginUserMapper = { result: LoginResp ->
        UserDataManager.instance.logout()
        UserDataManager.instance.setLogin(true)
        UserDataManager.instance.setToken(result.token)
        UserDataManager.instance.setSiteId(result.siteId)
        UserDataManager.instance.setUserNickName(result.nickname)
        CacheManager.instance.saveUserInfo(result)
        CacheManager.instance.saveData("TOKEN", result.token)
        result
    }
    private var isPad = false
    private val apiInterceptor: ApiInterceptor

    companion object {
        @JvmStatic
        val instance: ApiManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ApiManager()
        }
    }

    private constructor() {
        RxJavaPlugins.setErrorHandler {
            it.printStackTrace()
        }
        val builder = OkHttpClient.Builder()
        builder.connectTimeout(UrlConstatns.DEFAULT_CONNECT_TIME_OUT, TimeUnit.SECONDS)//连接超时时间
        builder.writeTimeout(UrlConstatns.DEFAULT_WRITE_TIME_OUT, TimeUnit.SECONDS)//写操作 超时时间
        builder.readTimeout(UrlConstatns.DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS)//读操作超时时间
        isPad = false
        apiInterceptor = ApiInterceptor(isPad)
        if (AppUtils.isDebug) {
            val loggerInterceptor = HttpLoggingInterceptor()
            loggerInterceptor.level = HttpLoggingInterceptor.Level.BODY
            builder.addInterceptor(loggerInterceptor)

            val trustManager = SSL.createX509TrustManager()
            if (trustManager == null) {
                builder.sslSocketFactory(SSL.createSSLSocketFactory())
            } else {
                builder.sslSocketFactory(SSL.createSSLSocketFactory(), trustManager)
            }

        } else {
            builder.proxy(Proxy.NO_PROXY)
        }

        builder.addInterceptor(apiInterceptor)
        val host = UrlConstatns.getHost()
        okHttpClient = builder.build()
        retrofit = Retrofit.Builder()
            .client(okHttpClient)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(host)
            .build()
        apiService = retrofit.create(ApiService::class.java)
    }

    fun postApiError(e: Throwable?, onFailListener: ((code: String, msg: String) -> Unit)) {
        if (e != null && e is ApiException) {
            if (TextUtils.equals("402", e.code)) {
                SupportBaseApplication.supportBaseApplication?.logout(e.code, e.msg)
            } else if (TextUtils.equals("auth.encryption_data_not_exist", e.code)) {
                // AES 失效后，重新获取AES密钥
                onFailListener.invoke(e.code, e.msg)
            } else {
                onFailListener.invoke(e.code, e.msg)
            }

        } else {
            val sb = StringBuilder("未知错误!")
            if (e != null) {
                sb.append("\n").append(e.getLocalizedMessage())
            }
            e?.printStackTrace()
            onFailListener.invoke(ExceptionEngine.UN_KNOWN_ERROR.toString(), sb.toString())
        }
    }

    /**
     * 登录
     */
    fun login(
        account: String, password: String, captcha: String, captchaId: Int, siteId: Int,
        onSuccessListener: (loginResp: LoginResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["account"] = account
        map["captcha"] = captcha
        map["password"] = password
        map["captchaId"] = captchaId
        map["siteId"] = siteId

        return apiService.login(map)
            .bindToHttpRequestListener(
                onSuccessListener,
                onFailListener,
                loginUserMapper,
                lifecycleOwner
            )
    }

    /**
     * 注册
     */
    fun register(
        account: String, password: String, captcha: String, captchaId: Int, name: String,
        phone: String, parentId: String,email: String,
        onSuccessListener: (loginResp: LoginResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["account"] = account //账户
        map["captcha"] = captcha //验证码
        map["password"] = password
        map["captchaId"] = captchaId //验证吗id
        map["name"] = name
        map["phone"] = phone
        map["email"] = email
        map["parentId"] = parentId//邀请码

        return apiService.register(map)
            .bindToHttpRequestListener(
                onSuccessListener,
                onFailListener,
                lifecycleOwner
            )
    }

    /**
     * 获取用户统计信息
     */
    fun getInfo(
        onSuccessListener: (loginResp: MyInfoResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return apiService.getInfo()
            .bindToHttpRequestListener(
                onSuccessListener,
                onFailListener,
                lifecycleOwner
            )
    }


    //**********************APP - 代付订单 *******************************************
    /**
     * 取消接单
     */
    fun cancelTake(
        code: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return apiService.cancelTake(code)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 我发起的
     */
    fun listMyCreate(
        current: Int, size: Int, orderDf: OrderDfListReq,
        onSuccessListener: (data: ListMyCreateResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDf

        return apiService.listMyCreate(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 我接单的
     */
    fun listMyTake(
        current: Int, size: Int, orderDf: OrderDfListReq,
        onSuccessListener: (data: ListMyCreateResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDf

        return apiService.listMyTake(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 接单池
     */
    fun listWaitTake(
        current: Int, size: Int, orderDf: OrderDfListReq,
        onSuccessListener: (data: ListMyCreateResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDf

        return apiService.listWaitTake(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 付款
     */
    fun dfPay(
        ckRemark: String, ckVoucherPath: String, code: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return Observable.just(ckVoucherPath)
            .flatMap { path ->
                val file = File(path)
                val mimeType = FileUtils.getMimeType(file.path)
                val mediaType: MediaType? =
                    MediaType.parse(if (TextUtils.isEmpty(mimeType)) "image/png" else mimeType)
                apiService.upload(
                    MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        RequestBody.create(mediaType, file)
                    )
                )
            }.map(ApiResponseFunction<ImagePath>())
            .flatMap { photo ->

                val map = HashMap<String, Any>()
                map["ckRemark"] = ckRemark
                map["ckVoucherPath"] = photo.path
                map["code"] = code
                apiService.dfPay(map)
            }
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)

    }

    /**
     * 重新 付款
     */
    fun rePay(
        ckRemark: String, ckVoucherPath: String, code: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return Observable.just(ckVoucherPath)
            .flatMap { path ->
                val file = File(path)
                val mimeType = FileUtils.getMimeType(file.path)
                val mediaType: MediaType? =
                    MediaType.parse(if (TextUtils.isEmpty(mimeType)) "image/png" else mimeType)
                apiService.upload(
                    MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        RequestBody.create(mediaType, file)
                    )
                )
            }.map(ApiResponseFunction<ImagePath>())
            .flatMap { photo ->

                val map = HashMap<String, Any>()
                map["ckRemark"] = ckRemark
                map["ckVoucherPath"] = photo.path
                map["code"] = code
                apiService.rePay(map)
            }
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 代付单处理过程
     */
    fun processList(
        current: Int, size: Int, orderDf: ProcessReq,
        onSuccessListener: (data: ProcessListResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDf
        return apiService.processList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 接单
     */
    fun dfTake(
        code: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return apiService.dfTake(code)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    //*************** 代收订单 *********************
    /**
     * 自动确认到账
     * content 消息内容
     * deviceId 手机序列号
     * msgNumber  比如银行发短信的号码95533  95555等，如果是支付宝和微信的，可以传alipay wechat
     */
    fun autoConfirm(
        content: String, deviceId: String, msgNumber: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit
    ): Disposable {
        val map = HashMap<String, Any>()
        map["content"] = content
        map["deviceId"] = deviceId
        map["msgNumber"] = msgNumber

        return apiService.autoConfirm(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener)
    }

    /**
     * 我发起的
     */
    fun listDsMyCreate(
        current: Int, size: Int, orderDs: OrderDsListReq,
        onSuccessListener: (data: ListMyCreateResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDs

        return apiService.listDsMyCreate(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 我接单的
     */
    fun listDsMyTake(
        current: Int, size: Int, orderDs: OrderDsListReq,
        onSuccessListener: (data: DsMyTakeListResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDs

        return apiService.listDsMyTake(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 接单池
     */
    fun listDsWaitTake(
        current: Int, size: Int, orderDs: OrderDsListReq,
        onSuccessListener: (data: DsWaitTake) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDs

        return apiService.listDsWaitTake(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 代付单处理过程
     */
    fun dsProcessList(
        current: Int, size: Int, orderDs: ProcessReq,
        onSuccessListener: (data: ProcessListResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = orderDs
        return apiService.dsProcessList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 接单
     */
    fun dsTake(
        code: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return apiService.dsTake(code)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 接单人 确认到账
     */
    fun dsTakeConfirm(
        code: String, amountAct: Double, skRemark: String, skVoucherPath: String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return Observable.just(skVoucherPath)
            .flatMap { path ->
                val file = File(path)
                val mimeType = FileUtils.getMimeType(file.path)
                val mediaType: MediaType? =
                    MediaType.parse(if (TextUtils.isEmpty(mimeType)) "image/png" else mimeType)
                apiService.upload(
                    MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        RequestBody.create(mediaType, file)
                    )
                )
            }.map(ApiResponseFunction<ImagePath>())
            .flatMap { photo ->
                val map = HashMap<String, Any>()
                map["code"] = code
                map["amountAct"] = amountAct
                map["skRemark"] = skRemark
                map["skVoucherPath"] = photo.path
                apiService.dsTakeConfirm(map)
            }
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)

    }
    //**************** 其他*********************
    /**
     * 佣金流水
     */
    fun brokerageFlow(
        current: Int, size: Int, req: BrokergeFlowReq,
        onSuccessListener: (data: BrokerageFlowResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = req
        return apiService.brokerageFlowList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 佣金流水
     */
    fun noticeList(
        current: Int, size: Int, req: NoticeReq,
        onSuccessListener: (data: NoticeResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = req
        return apiService.noticeList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 查询字典列表
     */
    fun dictionaryList(
        onSuccessListener: (data: Dict) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return apiService.dictionaryList()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    fun mySiteList(current: Int, size: Int, req: SiteListReq,
        onSuccessListener: (data: MySiteListResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = req
        return apiService.siteList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    fun tgm(
        onSuccessListener: (data: String) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return apiService.tgm()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 查询字典列表
     */
    fun dictionaryAll(
        onSuccessListener: (data: DictionarALlRep) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return apiService.dictionaryAll()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 上传图片
     */
    fun upload(
        filePath: String,
        onSuccessListener: (data: ImagePath) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return Observable.just(filePath)
            .flatMap { path ->
                val file = File(path)
                val mimeType = FileUtils.getMimeType(file.path)
                val mediaType: MediaType? =
                    MediaType.parse(if (TextUtils.isEmpty(mimeType)) "image/png" else mimeType)
                apiService.upload(
                    MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        RequestBody.create(mediaType, file)
                    )
                )
            }
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 代办
     */
    fun todoInfo(
        onSuccessListener: (data: TodoInfoResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return apiService.todoInfo()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }
    //*********** 商户 收款码 **********************
    /**
     * 添加收款码
     */

    fun addQrCode(
        qrCode: QrCodeReq, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return Observable.just(qrCode.skQrCode)
            .flatMap { path ->
                val file = File(path)
                val mimeType = FileUtils.getMimeType(file.path)
                val mediaType: MediaType? =
                    MediaType.parse(if (TextUtils.isEmpty(mimeType)) "image/png" else mimeType)
                apiService.upload(
                    MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        RequestBody.create(mediaType, file)
                    )
                )
            }.map(ApiResponseFunction<ImagePath>())
            .flatMap { photo ->
                val map = HashMap<String, Any>()
                map["channelId"] = qrCode.channelId
                map["dayAmountMax"] = qrCode.dayAmountMax
                map["oneAmountMax"] = qrCode.oneAmountMax
                map["oneAmountMin"] = qrCode.oneAmountMin
                map["remark"] = qrCode.remark
                map["siteId"] = qrCode.siteId
//                map["skBank"] = qrCode.skBank
                map["skName"] = qrCode.skName
                map["skNumber"] = qrCode.skNumber
                map["deviceId"] = qrCode.deviceId
                map["skQrCode"] = photo.path
                apiService.addQrCode(map)
            }
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)

    }

    fun addQrCodeBank(
        qrCode: QrCodeReq, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["channelId"] = qrCode.channelId
        map["dayAmountMax"] = qrCode.dayAmountMax
        map["oneAmountMax"] = qrCode.oneAmountMax
        map["oneAmountMin"] = qrCode.oneAmountMin
        map["remark"] = qrCode.remark
        map["siteId"] = qrCode.siteId
        map["skBank"] = qrCode.skBank
        map["skBankId"] = qrCode.skBankId
        map["skName"] = qrCode.skName
        map["skNumber"] = qrCode.skNumber
        map["deviceId"] = qrCode.deviceId
//        map["skQrCode"] = qrCode.skQrCode
        return apiService.addQrCode(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)

    }

    /**
     * 修改收款码
     */

    fun editQrCode(
        qrCode: QrCodeReq,id:Int, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return Observable.just(qrCode.skQrCode)
            .flatMap { path ->
                val file = File(path)
                val mimeType = FileUtils.getMimeType(file.path)
                val mediaType: MediaType? =
                    MediaType.parse(if (TextUtils.isEmpty(mimeType)) "image/png" else mimeType)
                apiService.upload(
                    MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        RequestBody.create(mediaType, file)
                    )
                )
            }.map(ApiResponseFunction<ImagePath>())
            .flatMap { photo ->
                val map = HashMap<String, Any>()
                map["id"] = id
                map["channelId"] = qrCode.channelId
                map["dayAmountMax"] = qrCode.dayAmountMax
                map["oneAmountMax"] = qrCode.oneAmountMax
                map["oneAmountMin"] = qrCode.oneAmountMin
                map["remark"] = qrCode.remark
                map["siteId"] = qrCode.siteId
//                map["skBank"] = qrCode.skBank
                map["skName"] = qrCode.skName
                map["skNumber"] = qrCode.skNumber
                map["deviceId"] = qrCode.deviceId
                map["skQrCode"] = photo.path
                apiService.editQrCode(map)
            }
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }
    fun editQrCodeBank(
        qrCode: QrCodeReq,id:Int, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["id"] = id
        map["channelId"] = qrCode.channelId
        map["dayAmountMax"] = qrCode.dayAmountMax
        map["oneAmountMax"] = qrCode.oneAmountMax
        map["oneAmountMin"] = qrCode.oneAmountMin
        map["remark"] = qrCode.remark
        map["siteId"] = qrCode.siteId
        map["skBank"] = qrCode.skBank
        map["skBankId"] = qrCode.skBankId
        map["skName"] = qrCode.skName
        map["skNumber"] = qrCode.skNumber
        map["skQrCode"] = qrCode.skQrCode
        map["deviceId"] = qrCode.deviceId

        return apiService.editQrCode(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 删除收款码
     */

    fun delQrCode(
        qrId: Int, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["id"] = qrId

        return apiService.delQrCode(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 绑定监听手机
     */

    fun editDevice(
        id: Int, deviceId: String, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["id"] = id
        map["deviceId"] = deviceId
        return apiService.editDevice(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 编辑状态
     */
    fun editStatus(
        ids: ArrayList<Int>, status: String, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["ids"] = ids
        map["status"] = status
        return apiService.editStatus(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 收款码 列表
     */

    fun qrCodeList(
        current: Int, size: Int, req: QrCodeListReq, onSuccessListener: (data: QrCodeResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = req

        return apiService.qrCodeList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }
    //*************商户*********************
    /**
     * 修改密码
     */
    fun editPwd(
        oldPwd: String, newPwd: String, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["newPassword"] = newPwd
        map["oldPassword"] = oldPwd
        return apiService.editPwd(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 修改用户信息
     */
    fun editUser(
        phone: String, nickname: String, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["nickname"] = nickname
        map["phone"] = phone
        return apiService.editUser(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 商户启动禁用状态开关
     */
    fun editUserStatus(
        id: Int, status: String, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["status"] = status
        map["id"] = id
        return apiService.editUserStatus(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 获取验证码
     */
    fun captcha(
        onSuccessListener: (data: CaptchaResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return apiService.captcha()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 账户流水
     */
    fun changeFlowList(
        current: Int,
        size: Int,
        req: ChangeFlowReq,
        onSuccessListener: (data: ChangeFlowResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = req
        return apiService.changeFlowList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 本人的所有通道
     */
    fun siteChannelList(
        onSuccessListener: (data: SiteChannelResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {

        return apiService.siteChannelList()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 编辑状态 代付
     */
    fun editDfStatus(
        status: String, id: Int, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["dfStatus"] = status
        map["id"] = id
        return apiService.siteChannelDfStatus(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 编辑状态 代收
     */
    fun editDsStatus(
        status: String, id: Int, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["dsStatus"] = status
        map["id"] = id
        return apiService.siteChannelDsStatus(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 银行列表
     */
    fun bankList(
        current: Int, size: Int, onSuccessListener: (data: BankListResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = OrderDfListReq()
        return apiService.bankList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    fun msgList(
        current: Int, size: Int, onSuccessListener: (data: MsgListResp) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["current"] = current
        map["size"] = size
        map["params"] = MsgListReq()
        return apiService.msgList(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 编辑本商户代收自动接单开关
     */
    fun autoTake(
        dsAutoTakeSwitch: String, onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        val map = HashMap<String, Any>()
        map["dsAutoTakeSwitch"] = dsAutoTakeSwitch
        return apiService.autoTake(map)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }
    /**
     * 加载代收自动接单开关
     */
    fun loadAutoTakeSwitch(
         onSuccessListener: (status:String) -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return apiService.loadAutoTakeSwitch()
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }

    /**
     * 加载代收自动接单开关
     */
    fun onlineDeviceID( deviceId:String,
        onSuccessListener: () -> Unit,
        onFailListener: (code: String, msg: String) -> Unit,
        lifecycleOwner: LifecycleOwner
    ): Disposable {
        return apiService.onlineDeviceId(deviceId)
            .bindToHttpRequestListener(onSuccessListener, onFailListener, lifecycleOwner)
    }
}